package com.sky.service.impl;


import com.sky.dto.GoodsSalesDTO;
import com.sky.mapper.OrderMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.sky.entity.Orders.COMPLETED;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    OrderMapper orderMapper;

    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        TurnoverReportVO turnoverReportVO = new TurnoverReportVO();
        StringBuilder dateList = new StringBuilder();
        StringBuilder turnoverList = new StringBuilder();
        for (LocalDate date = begin; date.isBefore(end.plusDays(1)); date = date.plusDays(1)) {
            dateList.append(date).append(",");
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();
            map.put("begin", beginTime);
            map.put("end", endTime);
            map.put("status", COMPLETED);
            Double turnover = orderMapper.sumByMap(map);
            if (turnover != null) {
                turnoverList.append(turnover).append(",");
            } else {
                turnoverList.append(0).append(",");
            }
        }
        dateList.deleteCharAt(dateList.length() - 1);
        String dateListStr = dateList.toString();
        turnoverReportVO.setDateList(dateListStr);
        turnoverList.deleteCharAt(turnoverList.length() - 1);
        String turnoverListStr = turnoverList.toString();
        turnoverReportVO.setTurnoverList(turnoverListStr);
        return turnoverReportVO;
    }

    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        StringBuilder dateList = new StringBuilder();
        StringBuilder totalUserList = new StringBuilder();
        StringBuilder todayUserList = new StringBuilder();
        for (LocalDate date = begin; date.isBefore(end.plusDays(1)); date = date.plusDays(1)) {
            dateList.append(date).append(",");
            totalUserList.append(orderMapper.countuser(LocalDateTime.of(LocalDate.MIN, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX))).append(",");
            todayUserList.append(orderMapper.countuser(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX))).append(",");
        }
        dateList.deleteCharAt(dateList.length() - 1);
        totalUserList.deleteCharAt(totalUserList.length() - 1);
        todayUserList.deleteCharAt(todayUserList.length() - 1);
        String dateListStr = dateList.toString();
        String totalUserListStr = totalUserList.toString();
        String todayUserListStr = todayUserList.toString();
        UserReportVO userReportVO = new UserReportVO();
        userReportVO.setDateList(dateListStr);
        userReportVO.setTotalUserList(totalUserListStr);
        userReportVO.setNewUserList(todayUserListStr);
        return userReportVO;
    }

    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        StringBuilder dateList = new StringBuilder();
        StringBuilder orderList = new StringBuilder();
        StringBuilder validOrderList = new StringBuilder();
        Integer totalOrderNumber = 0;
        Integer validOrderNumber = 0;
        for (LocalDate date = begin; date.isBefore(end.plusDays(1)); date = date.plusDays(1)) {
            dateList.append(date).append(",");
            Integer tmp1 = orderMapper.countOrder(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX), null);
            Integer tmp2 = orderMapper.countOrder(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX), COMPLETED);
            totalOrderNumber += tmp1;
            validOrderNumber += tmp2;
            orderList.append(tmp1).append(",");
            validOrderList.append(tmp2).append(",");
        }
        dateList.deleteCharAt(dateList.length() - 1);
        orderList.deleteCharAt(orderList.length() - 1);
        validOrderList.deleteCharAt(validOrderList.length() - 1);
        String dateListStr = dateList.toString();
        String orderListStr = orderList.toString();
        String validOrderListStr = validOrderList.toString();
        OrderReportVO orderReportVO = new OrderReportVO();
        orderReportVO.setDateList(dateListStr);
        orderReportVO.setOrderCountList(orderListStr);
        orderReportVO.setValidOrderCountList(validOrderListStr);
        if (totalOrderNumber == 0) orderReportVO.setOrderCompletionRate(0.0);
        else orderReportVO.setOrderCompletionRate(validOrderNumber * 1.0 / totalOrderNumber);
        orderReportVO.setTotalOrderCount(totalOrderNumber);
        orderReportVO.setValidOrderCount(validOrderNumber);
        return orderReportVO;

    }
    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {

        LocalDateTime ks = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime js = LocalDateTime.of(end, LocalTime.MAX);
        System.out.println(ks);
        System.out.println(js);
        List<GoodsSalesDTO> salestop10 = orderMapper.getSalestop10(ks, js);
//        System.out.println("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq");
        List<String> names = salestop10.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        String nameList = StringUtils.join(names, ",");
        List<Integer> numbers = salestop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        String numberList = StringUtils.join(numbers, ",");
        SalesTop10ReportVO salesTop10ReportVO = new SalesTop10ReportVO();
        salesTop10ReportVO.setNameList(nameList);
        salesTop10ReportVO.setNumberList(numberList);
        return salesTop10ReportVO;
    }


}
